import React,{ Component } from 'react'
import { Modal,Tree,message } from 'antd'
import { move_node } from '../../../actions/disk'


const TreeNode = Tree.TreeNode

class MCModal extends Component {

  constructor(props){
    super(props)
    this.TreeData = Array(0)
    this.Dirs = null
    this.state = {
      selectkey:null
    }
  }

  onSelect = (selectedKeys, info) => {
    this.setState({
      selectkey:selectedKeys,
    })
  }

  onSumbit = () => {
    const { dispatch } = this.props
    if(this.state.selectkey === null){
      message.error('未指定目录')
    }else {
        switch (this.props.action) {
          case '移动':
            dispatch(move_node(dispatch,this.props.parent,this.props.stack,this.state.selectkey))
            break
        default :
            console.log('fail')
        }
        this.props.ok()
    }
  }

  generateData = (_data, _parent, _gData) => {
    let parent = _parent || '0'
    let rData = _gData || this.TreeData
    let index = 0
    for(let key = 0;key<_data.length;key++){
      if(_data[key] !== undefined){
        if(_data[key].parent === parent){
          rData.push({
            'key':_data[key].directory,
            'tag':_data[key].tag,
            'children':Array(0)
          })
          let dir = _data[key].directory
          delete _data[key]
          this.generateData(_data,dir,rData[index].children)
          index++
        }
      }
    }
  }


  render(){
    this.TreeData = Array(0)
    this.Dirs = Object.assign(Array(0),this.props.Dirs)
    this.generateData(this.Dirs)
    const loop = data => data.map((item) => {
      return (
        <TreeNode key={item.key} title={item.tag} >
        {loop(item.children)}
        </TreeNode>
      )
    })
    return (
      <Modal
      title={this.props.count(this.props.action)}
      visible={this.props.visible}
      onOk={this.onSumbit}
      onCancel={this.props.cancel}
      width={400}
      >
      <Tree
      showLine
      defaultExpandedKeys={[this.props.rootDir]}
      onSelect={this.onSelect}
      >
      {loop(this.TreeData)}
      </Tree>
      </Modal>
    )
  }
}

export default MCModal
